package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.ExportDao;
import cn.learn.dao.cargo.PackingListDao;
import cn.learn.domain.cargo.Export;
import cn.learn.domain.cargo.PackingList;
import cn.learn.domain.cargo.PackingListExample;
import cn.learn.service.cargo.PackingListService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class PackingListServiceImpl implements PackingListService {
    @Autowired
    private PackingListDao packingListDao;
    @Autowired
    private ExportDao exportDao;

    @Override
    public PageInfo findAll(PackingListExample packingListExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PackingList> packingLists = packingListDao.selectByExample(packingListExample);
        PageInfo pageInfo = new PageInfo(packingLists);
        return pageInfo;
    }

    @Override
    public void save(PackingList packingList) {
        String exportIds = packingList.getExportIds();
        String[] exportIdArr = exportIds.split(",");
        StringBuffer buffer = new StringBuffer();
        for (String exportId : exportIdArr) {
            Export export = exportDao.selectByPrimaryKey(exportId);
            export.setState(3);
            buffer.append(export.getCustomerContract()).append(" ");
            exportDao.updateByPrimaryKeySelective(export);
        }
        packingList.setExportNos(buffer.toString());
        packingListDao.insertSelective(packingList);
    }

    @Override
    public PackingList findById(String id) {
        PackingList packingList = packingListDao.selectByPrimaryKey(id);
        return packingList;
    }

    @Override
    public void update(PackingList packingList) {
        packingListDao.updateByPrimaryKeySelective(packingList);
    }

    @Override
    public void delete(String id) {
        packingListDao.deleteByPrimaryKey(id);
    }
}
